#include <string>
#include <vector>
using namespace std;
class Solution
{
public:
    int compress(vector<char> &chars)
    {
        string res;
        if (!chars.size())
            return 0;
        pair<char, int> count = {chars[0], 1};
        int i = 0;
        while (++i)
        {
            if (i < chars.size() && chars[i] == chars[i - 1])
                count.second++;
            else
            {
                res += count.first;
                if (count.second != 1)
                    res += to_string(count.second);
                if (i >= chars.size())
                    break;
                count = {chars[i], 1};
            }
        }
        chars.clear();
        for (auto ch : res)
            chars.emplace_back(ch);
        return chars.size();
    }
};